package com.lzb.compressmaster.activity;

import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.lzb.compressmaster.BaseAc;
import com.lzb.compressmaster.Content;
import com.lzb.compressmaster.R;
import com.lzb.compressmaster.basic.utils.SPUtil;
import com.lzb.compressmaster.databinding.ActivityForgetPasswordBinding;

import java.util.Random;

public class ForgetPasswordActivity extends BaseAc<ActivityForgetPasswordBinding> implements View.OnClickListener {

    private String question, answer;
    private int position;

    @Override
    protected int onCreate() {
        return R.layout.activity_forget_password;
    }

    @Override
    protected void initView() {
        mDataBinding.ivForgetPwdBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.tvConfirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tvConfirm) {//确定
            String inputAnswer = mDataBinding.etAnswer.getText().toString().trim();
            if (TextUtils.isEmpty(inputAnswer)) {
                ToastUtils.showShort(getString(R.string.please_input_question_answer));
                return;
            }
            if(!inputAnswer.equals(answer)){
                ToastUtils.showShort(getString(R.string.answer_error));
                return;
            }
            startActivity(ResetPwdActivity.class);
            finish();
        }
    }

    @Override
    protected void initData() {
        int num = 1;
        String question1 = SPUtil.getString(mContext, Content.myQuestion1, "");
        String question2 = SPUtil.getString(mContext, Content.myQuestion2, "");
        String question3 = SPUtil.getString(mContext, Content.myQuestion3, "");
        String answer1 = SPUtil.getString(mContext, Content.myAnswer1, "");
        String answer2 = SPUtil.getString(mContext, Content.myAnswer2, "");
        String answer3 = SPUtil.getString(mContext, Content.myAnswer3, "");
        if (!TextUtils.isEmpty(question3)) {
            num = 3;
        } else if (!TextUtils.isEmpty(question2)) {
            num = 2;
        }
        Random random = new Random();
        position = random.nextInt(num);
        if (position == 0) {
            question = question1;
            answer = answer1;
        } else if (position == 1) {
            question = question2;
            answer = answer2;
        } else if (position == 2) {
            question = question3;
            answer = answer3;
        }
        mDataBinding.tvQuestion.setText(question);
    }
}